package bigo.todolist.avocado;


import android.os.Bundle;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.Editable;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/** 
 * Register page
 * @author Big Ohvocodo Task Force
 */
public class Register extends Activity implements View.OnClickListener{
	Button register;
	Button cancel;
	EditText username;
	EditText password;
	EditText passwordConf;
	
	/**
	 * Initializations for when page loads
	 */
	protected void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState); 
			setContentView(R.layout.register); //Set the layout of the window
			
			idSet(); //Call method to set the ids for each variable
			
			register.setOnClickListener(this); //Create click listener for register button
			cancel.setOnClickListener(this);
		}
	
	/**
	 * Instantiate UI variables
	 */
	private void idSet() {
		register = (Button) findViewById(R.id.registerButton); //Set id
		cancel = (Button) findViewById(R.id.cancelButton); //Set id
		username = (EditText) findViewById(R.id.usernameInput); //Set id
		password = (EditText) findViewById(R.id.passwordInput); //Set id
		passwordConf = (EditText) findViewById(R.id.passwordConfirmationInput); //Set id
	}
	
	/**
	 * Checks for valid username/password
	 * @return boolean representing if fields are filled correctly
	 */
	public boolean checkTextFields(String userText, String passwordText, String passwordConfText) {
		if(userText.length() == 0 || passwordText.length() == 0 || passwordConfText.length() == 0) {
			Toast.makeText(Register.this,"Please fill all fields", Toast.LENGTH_LONG).show(); //Alert to the user
			return false;
		} else {
			return true;
		}
	}
	
	/**
	 * Checks that passwords match
	 * @param passwordText
	 * @param passwordConfText
	 * @return
	 */
	public boolean checkMatchingPasswords(String passwordText, String passwordConfText) {
		if (passwordText.equals(passwordConfText) != true) {
			Toast.makeText(Register.this,"Passwords do not match", Toast.LENGTH_LONG).show(); //Alert to the user
			return false;
		} else {
			return true;
		}
	}
	
	
	/**
	 * Actions to perform when given view is clicked
	 * 
	 * @param view - the view that was clicked upon
	 */
	public void onClick(View view) {
		
		Editable user = username.getText(); 
		Editable passwd = password.getText();
		Editable passwdConf = passwordConf.getText();
		String userText = user.toString(); //Gets the username from the EditText
		String passwordText = passwd.toString(); //Gets the password from EditText
		String passwordConfText = passwdConf.toString();
		
		
		switch(view.getId())
		{
			case R.id.cancelButton: //Case for pressing the cancel position
		
			try {
				Class ourClass = Class.forName("bigo.todolist.avocado.Login");
				Intent ourIntent = new Intent(Register.this, ourClass);
				startActivity(ourIntent);
			} catch(Exception e)
			{
				Toast.makeText(Register.this,e.getMessage(), Toast.LENGTH_LONG).show();
			}
			break;
			
			case R.id.registerButton: //Case for pressing the register button
				try{
					
					if (checkTextFields(userText, passwordText, passwordConfText) == false || checkMatchingPasswords(passwordText, passwordConfText) == false) {
					} else {
						DBUserAdapter dbUser = new DBUserAdapter(Register.this);
						dbUser.open();
						dbUser.AddUser(userText,passwordText); //"Register" or Add user to database
						dbUser.close();
						//pop message
						Toast.makeText(Register.this,"Registered successfully", Toast.LENGTH_LONG).show();
						//switch back to login page
						Class ourClass = Class.forName("bigo.todolist.avocado.Login");
						Intent ourIntent = new Intent(Register.this, ourClass);
						startActivity(ourIntent);
					}
					
				}catch(Exception e)
				{
					Toast.makeText(Register.this,e.getMessage(), Toast.LENGTH_LONG).show();
				}
			break;
		} 	
			
		}
	}

